package com.ruoyi.cderp.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

//图片上传控制器
@RestController
public class FileUploadController {

    @Autowired
    private OssService ossService;

    @PostMapping("/file/upload")
    public AjaxResult uploadImage(@RequestParam("file") MultipartFile file) {
        try (InputStream inputStream = file.getInputStream()) {  // 自动关闭流
            String imageUrl = ossService.uploadFile(file);
            return AjaxResult.success("上传成功", imageUrl);
        } catch (Exception e) {
            return AjaxResult.error("上传失败: " + e.getMessage());
        }
    }
}
